// pages/editShopItem/editShopItem.js
const app = getApp()
var common = require('../../utils/common.js');
Page({
  data: {
    items:[],
    freeitems:[],
    checked: [],
    freechecked:[],
    checkedSkuid:[],
    checkedfreeSkuid:[],
    thisChecked: false,
    thisfreeChecked: false,
    noAll: true,
    loading: true,
    current: 0,
    page: 1,
    freepage: 1,
    freeloading: true
  },
  onLoad: function () {
    const that = this
    // const eventChannel = this.getOpenerEventChannel()
    // eventChannel.on('acceptDataFromOpenerPage', function(data) {
    //   console.log(data)
    //   if(data.shopInfo.isAutoChoose){
    //     that.getFreeItem()
    //     that.setData({
    //       current:1
    //     })
    //   }else{
        
    //   }
    //   that.getShopItem()
    //   that.getFreeItem()
    // })
    this.getShopItem()
    this.getFreeItem()
    
  },
  deleteItem(){
    const that = this;
    var checkedSkuid = []
    if(this.data.current == 0){
      checkedSkuid = this.data.checkedSkuid
    }else{
      checkedSkuid = this.data.checkedfreeSkuid
    }
    if (checkedSkuid.length > 0){
      common.post({ url: '/app/v2/shop/delProduct', data: { sessionId: app.globalData.sessionId, skuId: checkedSkuid } })
        .then((res) => {
          console.log(res.data)
          if (res.data.code == 0) {
            wx.showLoading({
              title: '正在删除中',
            })
            if(this.data.current == 0){
              that.setData({
                checkedSkuid: [],
                thisChecked: false
              })
              that.getShopItem()
            }else{
              that.setData({
                checkedfreeSkuid: [],
                thisfreeChecked: false
              })
              that.getFreeItem()
            }
          } else {
            console.log(res.data)
          }
        }).catch((res) => {
          console.log(res)

        })
    }else{
      wx.showToast({
        title: '还没有选择商品',
        icon: 'none',
        duration: 2000
      })
    }
  },
  allSelect(){
    console.log('all')
    
    var item = []
    var checkedSkuid = []
    var checked = []
    if(this.data.current == 0){
      item = this.data.items
      this.setData({
        thisChecked: true,
        noAll1: false
      })
    }else{
      item = this.data.freeitems
      this.setData({
        thisChecked: true,
        noAll2: false
      })
    }
    console.log(this.data.items)
     for (let i = 0; i < item.length; i++) {
      checked[i] = true
      checkedSkuid[i] = item[i].skuId
    }
    if(this.data.current == 0){
      this.setData({
        checkedSkuid: checkedSkuid
      })
      this.setData({
        checked: checked
      })
      console.log(this.data.checked)
      console.log(this.data.checkedSkuid)
    }else{
      this.setData({
        checkedfreeSkuid: checkedSkuid
      })
      this.setData({
        freechecked: checked
      })
      console.log(this.data.freechecked)
      console.log(this.data.checkedfreeSkuid)
    }
    
  },
  cancelSelect() {
    console.log('cancelall')
    if(this.data.current == 0){
      this.setData({
        thisChecked: false,
        noAll1: true,
        checkedSkuid: []
      })
      console.log(this.data.items)
      for (let i = 0; i < this.data.items.length; i++) {
        this.data.checked[i] = false
      }
      this.setData({
        checked: this.data.checked
      })
      console.log(this.data.checked)
      console.log(this.data.checkedSkuid)
    }else{
      this.setData({
        thisfreeChecked: false,
        noAll2: true,
        checkedfreeSkuid: []
      })
      console.log(this.data.freeitems)
      for (let i = 0; i < this.data.freeitems.length; i++) {
        this.data.freechecked[i] = false
      }
      this.setData({
        freechecked: this.data.freechecked
      })
      console.log(this.data.freechecked)
      console.log(this.data.checkedfreeSkuid)
    }
  },
  checkboxChange1(e){
    this.setData({
      checkedSkuid: e.detail.value
    })
    console.log(this.data.checkedSkuid)
  },
  checkboxChange2(e){
    this.setData({
      checkedfreeSkuid: e.detail.value
    })
    console.log(this.data.checkedSkuid)
  },
  getCheckidx1(e){
    console.log(this.data.checked)
    console.log(e)
    const that = this
    console.log(e.currentTarget.dataset.idx)
    var idx = e.currentTarget.dataset.idx
    console.log(this.data.checked[idx])
    if (this.data.checked[idx]){
      this.data.checked[idx] = false
      this.setData({
        checked: that.data.checked
      })
    }else{
      this.data.checked[idx] = true
      this.setData({
        checked: that.data.checked
      })
    }
    
    console.log(that.data.checked)
    console.log(that.data.freechecked)
    
  },
  getCheckidx2(e){
    console.log(this.data.freechecked)
    console.log(e)
    const that = this
    console.log(e.currentTarget.dataset.idx)
    var idx = e.currentTarget.dataset.idx
    console.log(this.data.freechecked[idx])
    if (this.data.freechecked[idx]){
      this.data.freechecked[idx] = false
      this.setData({
        freechecked: that.data.freechecked
      })
    }else{
      this.data.freechecked[idx] = true
      this.setData({
        freechecked: that.data.freechecked
      })
    }
    console.log(that.data.checked)
    console.log(that.data.freechecked)
    
  },
  getShopItem() {
    const that = this
    common.post({
      url: '/app/v2/shop/list', data: { sessionId: app.globalData.sessionId, userId: app.globalData.userId, pageNo:1 }
    })
      .then((res) => {
        console.log(res.data)
        if (res.data.code == "0") {
          console.log(res.data.total)
          for (let i=0; i<res.data.total; i++){
            that.data.checked[i] = false
            that.setData({
              checked: that.data.checked
            })
            console.log(that.data.checked)
          }
          if(res.data.total == 0){
            that.setData({
              current:1,
            })
          }
          if(res.data.total < 11){
            that.setData({
              loading: false,
            })
          }
          that.setData({
            items: res.data.data,
            itemIsShow: true,
          })
          wx.hideLoading()
        } else if (res.data.code == "-9998") {
          that.loginTip()
        } else {
          console.log(res)
        }
      }).catch((res) => {
        console.log(res)
      })
  },
  getShopItemPage() {
    const that = this;
    var page = that.data.page + 1;
    common.post({ url: '/app/v2/shop/list', data: { sessionId: app.globalData.sessionId, userId: app.globalData.userId, pageNo: page } })
      .then((res) => {
        var data = that.data.items;
        console.log(res.data)
        if (that.data.page * 10 < res.data.total) {
          that.setData({
            page: page
          });
          var postList = res.data.data;
          for (let i = 0; i < postList.length; i++) {
            let tmp = postList[i];
            data.push(tmp);
            console.log(data)
            that.setData({
              items: data,
            })
            that.data.checked[i + (page-1)*10] = false
            that.setData({
              checked: that.data.checked
            })
            console.log(that.data.checked)
          }
        } else {
          console.log('没有了')
          wx.showToast({
            title: '没有了',
            icon: 'none',
            duration: 2000
          })
          that.setData({
            loading: false,
          })
        }

      }).catch((res) => {
        console.log(res)

      })
  },
  getFreeItem() {
    const that = this
    common.post({
      url: '/app/v2/shop/free99', data: { sessionId: app.globalData.sessionId, userId: app.globalData.userId, pageNo:1 }
    })
      .then((res) => {
        console.log(res.data)
        if (res.data.code == 0) {
          console.log(res.data.total)
          for (let i=0; i<res.data.total; i++){
            that.data.freechecked[i] = false
            that.setData({
              freechecked: that.data.freechecked
            })
            console.log(that.data.freechecked)
          }
          if(res.data.total < 11){
            that.setData({
              freeloading: false,
            })
          }
          that.setData({
            freeitems: res.data.data,
            itemIsShow: true,
          })
          wx.hideLoading()
        } else if (res.data.code == "-9998") {
          that.loginTip()
        } else {
          console.log(res)
        }
      }).catch((res) => {
        console.log(res)
      })
  },
  getFreeItemPage() {
    const that = this;
    var page = that.data.freepage + 1;
    common.post({ url: '/app/v2/shop/free99', data: { sessionId: app.globalData.sessionId, userId: app.globalData.userId, pageNo: page } })
      .then((res) => {
        var data = that.data.freeitems;
        console.log(res.data)
        if (that.data.freepage * 10 < res.data.total) {
          that.setData({
            freepage: page
          });
          var postList = res.data.data;
          for (let i = 0; i < postList.length; i++) {
            let tmp = postList[i];
            data.push(tmp);
            console.log(data)
            that.setData({
              freeitems: data,
            })
            that.data.freechecked[i + (page-1)*9] = false
            that.setData({
              freechecked: that.data.freechecked
            })
            console.log(that.data.freechecked)
          }
        } else {
          console.log('没有了')
          wx.showToast({
            title: '没有了',
            icon: 'none',
            duration: 2000
          })
          that.setData({
            freeloading: false,
          })
        }

      }).catch((res) => {
        console.log(res)

      })
  },
  onReachBottom() {
    if(this.data.current == 0){
      this.getShopItemPage()
    }else{
      this.getFreeItemPage()
    }
    
  },
  swichNav(e) {
    console.log(e.currentTarget.dataset.current)
    this.setData({
      current: e.currentTarget.dataset.current,
      
    });
  },
})